import jsreport from '@jsreport/browser-client'
import { Component, OnInit, Input, Output, EventEmitter, ElementRef } from '@angular/core';
import { ReporttempltService } from './services/reporttemplt.service';
@Component({
  selector: 'reporttemplt',
  templateUrl: './reporttemplt.component.html',
  styleUrls: ['./reporttemplt.component.scss']
})

export class ReporttempltComponent implements OnInit {

  @Output() downcallback = new EventEmitter();
  @Input() resinfor: any;
  @Input() downloading: boolean = false;

  jsreport: any = jsreport;
  logebase: any;
  _templete: any;

  res: any = {
    baseInfor: {
      service_name: "",
      customer_name: "",
      customer_age: "",
      customer_six: "",
      customer_phone: "",
      doctor_name: "",
      check_number: "",
      check_date: "",
    }
  };

  constructor(private el: ElementRef, private tplservice: ReporttempltService) { }

  ngOnInit() {
    this.logebase = this.tplservice.getLogeBase();
    this.formateInfor();
  }

  formateInfor() {

    for (const item in this.resinfor) {

      // 基本信息
      if (item === 'one_bgjd') {

        this.resinfor[item].forEach((element: any) => {
          this.res.baseInfor[element.name] = element.value;

          if(element.name==="customer_phone"){

            this.res.baseInfor[element.name] =element.value.substr(0, 3) + "****" + element.value.substr(7) ;
          }
        });
      }

      if (item === 'eight_contrast') {
        this.resinfor[item].checkList.forEach((element: any) => {
          let _index: any = 0;
          element.forEach((_elmt: any, index: any) => {
            if (_elmt.name === '本次结果') {
              _index = index;
            }
            if (_elmt.name === '上次结果') {
              if (_elmt.value > parseFloat(element[_index].value)) {
                element[_index].isHeight = false;
              } else {
                element[_index].isHeight = true;
              }

            }
          });

        });

      }

    }

  }

  upadateInfor() {
    this.formateInfor();
  }

  jsreportRender() {

    // this.jsreport.serverUrl = 'http://localhost:5488';
    this.jsreport.serverUrl = 'https://qcloud.services.dev.hygeian.com.cn/jsreport';

    this._templete = this.el.nativeElement.querySelector('#tpl').innerHTML;

    return new Promise((resolve, reject) => {

      let _tmp = this.tplservice.getDefaultHtml(this._templete);

      const report = jsreport.render({
        template: {
          content: _tmp,
          engine: 'handlebars',
          recipe: 'chrome-pdf',
          chrome: {
            marginTop: "120px",
            marginBottom:"30px",
            displayHeaderFooter: true,
            headerTemplate: this.tplservice.getHeader()
          }
        },
        data: {
          someData: 'browser client'
        }
      })
      resolve(report)
    })
  }

  jsreportDown() {
    this.jsreportRender().then((res: any) => {
      console.log(res);
      this.downcallback.emit(false);
      res.download('汉吉健康管理.pdf')

    })

  }


}
